#ifndef NODE_H
#define NODE_H
#include <iostream>

using namespace std;

class Node
{
public:
	Node();
	~Node();
	Node *SearchNode(int nodeIndex);
	void DeleteNode();
	void PreordeTraverse();
	void InorderTraverse();
	void PostorderTraverse();
	int index;
	//int data;
	int flag;
	Node *p1Child;
	Node *p2Child;
	Node *p3Child;
	Node *p4Child;
	Node *pParent;

private:

};

Node::Node()
{
	index = 0;
	//data = 0;
	flag = 0;
	p1Child = NULL;
	p2Child = NULL;
	p3Child = NULL;
	p4Child = NULL;
	pParent = NULL;
}

Node::~Node()
{
}

Node *Node::SearchNode(int nodeIndex)
{
	Node *temp = NULL;
	if (this->index == nodeIndex)
	{
		return this;
	}
	else if (this->p1Child != NULL)
	{
		if (this->p1Child->index == nodeIndex)
		{
			return this->p1Child;
		}
		else
		{
			temp = this->p1Child->SearchNode(nodeIndex);
			if (temp != NULL)
			{
				return temp;
			}
		}

	}
	 if (this->p2Child != NULL)
	{
		if (this->p2Child->index == nodeIndex)
		{
			return this->p2Child;
		}
		else
		{
			temp = this->p2Child->SearchNode(nodeIndex);
			if (temp != NULL)
			{
				return temp;
			}
		}

	}

	 if (this->p3Child != NULL)
	{
		if (this->p3Child->index == nodeIndex)
		{
			return this->p3Child;
		}
		else
		{
			temp = this->p3Child->SearchNode(nodeIndex);
			if (temp != NULL)
			{
				return temp;
			}
		}

	}
	  if (this->p4Child != NULL)
	{
		if (this->p4Child->index == nodeIndex)
		{
			return this->p4Child;
		}
		else
		{
			temp = this->p4Child->SearchNode(nodeIndex);
			if (temp != NULL)
			{
				return temp;
			}
		}

	}
	return NULL;
}

void Node::DeleteNode()
{
	if (this->p1Child != NULL)
	{
		this->p1Child->DeleteNode();
	}
	if (this->p2Child != NULL)
	{
		this->p2Child->DeleteNode();
	}
	if (this->p3Child != NULL)
	{
		this->p3Child->DeleteNode();
	}
	if (this->p4Child != NULL)
	{
		this->p4Child->DeleteNode();
	}
	if (this->pParent != NULL)
	{
		if (this->pParent->p1Child == this)
		{
			this->pParent->p1Child = NULL;
		}
		if (this->pParent->p2Child == this)
		{
			this->pParent->p2Child = NULL;
		}
		if (this->pParent->p3Child == this)
		{
			this->pParent->p3Child = NULL;
		}
		if (this->pParent->p4Child == this)
		{
			this->pParent->p4Child = NULL;
		}
	}

	delete this;
}

void Node::PreordeTraverse()
{
	cout << this->index << " " << this->flag << endl;
	if (this->p1Child != NULL)
	{
		this->p1Child->PreordeTraverse();
	}
	if (this->p2Child != NULL)
	{
		this->p2Child->PreordeTraverse();
	}
	if (this->p3Child != NULL)
	{
		this->p3Child->PreordeTraverse();
	}
	if (this->p4Child != NULL)
	{
		this->p4Child->PreordeTraverse();
	}
}

void Node::InorderTraverse()
{
	if (this->p1Child != NULL)
	{
		this->p1Child->InorderTraverse();
	}
	cout << this->index << " " << this->flag << endl;

	if (this->p2Child != NULL)
	{
		this->p2Child->InorderTraverse();
	}
	if (this->p3Child != NULL)
	{
		this->p3Child->InorderTraverse();
	}
	if (this->p4Child != NULL)
	{
		this->p4Child->InorderTraverse();
	}
}

void Node::PostorderTraverse()
{
	if (this->p1Child != NULL)
	{
		this->p1Child->PostorderTraverse();
	}
	if (this->p2Child != NULL)
	{
		this->p2Child->PostorderTraverse();
	}
	if (this->p3Child != NULL)
	{
		this->p3Child->PostorderTraverse();
	}
	if (this->p4Child != NULL)
	{
		this->p4Child->PostorderTraverse();
	}
}

#endif // !NODE_H